import 'package:flutter/material.dart';

class LongPressCircleButton extends StatefulWidget {
  final VoidCallback onLongPress;
  final Color color;
  final String text;
  final double size;
  const LongPressCircleButton(
      {super.key,
      required this.onLongPress,
      required this.color,
      required this.text,
      this.size = 80});
  @override
  State<LongPressCircleButton> createState() => _LongPressCircleButtonState();
}

class _LongPressCircleButtonState extends State<LongPressCircleButton> {
  double progress = 0;
  late final int durationMs = 1200;
  late final int tickMs = 20;
  late final double step = tickMs / durationMs;
  late bool pressed = false;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onLongPressStart: (_) {
        pressed = true;
        progress = 0;
        Future.doWhile(() async {
          if (!pressed) return false;
          await Future.delayed(Duration(milliseconds: tickMs));
          setState(() {
            progress += step;
            if (progress >= 1) {
              progress = 1;
              pressed = false;
              widget.onLongPress();
            }
          });
          return pressed && progress < 1;
        });
      },
      onLongPressEnd: (_) {
        pressed = false;
        setState(() {
          progress = 0;
        });
      },
      child: Stack(
        alignment: Alignment.center,
        children: [
          SizedBox(
            width: widget.size,
            height: widget.size,
            child: CircularProgressIndicator(
              value: progress,
              strokeWidth: 6,
              color: widget.color,
              backgroundColor: widget.color.withOpacity(0.2),
            ),
          ),
          Container(
            width: widget.size - 10,
            height: widget.size - 10,
            decoration: BoxDecoration(
              color: widget.color,
              shape: BoxShape.circle,
            ),
            alignment: Alignment.center,
            child:
                Text(widget.text, style: const TextStyle(color: Colors.white)),
          ),
        ],
      ),
    );
  }
}
